#!/bin/bash

green="\033[42;37m 启动docker... \033[0m"
red="\033[41;37m  \033[0m"

function print()
{
	echo ""
	if [ "$2" = "" ];then
		echo -e "\033[42;37m $1 \033[0m"
	else
		echo -e "\033[41;37m $1 \033[0m"
	fi
}
ip=$1
if [ "$ip" = "" ];then
	print ip不能为空 1
	exit 1
fi 

#安装docker
print 安装docker...
yum install -y docker-io

#配置加速
print 配置加速...
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://gejmpgfw.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

#拉取镜像
print 拉取镜像...
docker pull centos
print 配置网络...
#配置网络
service docker stop
ifconfig docker0 down
brctl delbr docker0
yum install -y bridge-utils

mv /etc/sysconfig/network-scripts/ifcfg-enp0s3 /etc/sysconfig/network-scripts/bak_ifcfg-enp0s3

tee /etc/sysconfig/network-scripts/ifcfg-enp0s3 <<-EOF
DEVICE=enp0s3
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
BRIDGE="br0"
EOF
ip=$1
tee /etc/sysconfig/network-scripts/ifcfg-br0 <<-EOF
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=static
IPADDR=${ip}
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1="114.114.114.114"
EOF

#修改docker网络类型
print 修改docker网络类型...
tee /etc/sysconfig/docker <<-EOF
other_args="-b=br0"
OPTIONS='--selinux-enabled -b=br0 --fixed-cidr=192.168.100.235/26'
EOF
yum install -y git
cd /usr/local/ && git clone https://github.com/jpetazzo/pipework
cp /usr/local/pipework/pipework /usr/local/bin/
echo -e "\033[42;37m 启动docker... \033[0m"
service docker restart

print [success]docker环境已经初始化完成[请重启机器]